home *** CD-ROM | disk | FTP | other *** search
/ Delphi Developer's Kit 1996 / Delphi Developer's Kit 1996.iso / power / dfm2txt / ufile.pas < prev   
Encoding:
Pascal/Delphi Source File  |  1995-12-22  |  6.4 KB  |  272 lines

  1. {    Last change:  JW    1 Sep 95    8:25 am    }
  2. unit Ufile;
  3.  
  4. interface
  5.  
  6. uses
  7.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  8.   Forms, Dialogs, StdCtrls, FileCtrl, ExtCtrls;
  9.  
  10. type
  11.  
  12.   TDFMOrTXT = (ConvertToForm, ConvertToText) ;
  13.  
  14.   TForm1 = class(TForm)
  15.     ResultLabel: TLabel;
  16.     StatusLabel: TLabel;
  17.     Wizard: TNotebook;
  18.     RadioGroup2: TRadioGroup;
  19.     GroupBox1: TGroupBox;
  20.     FileListBox1: TFileListBox;
  21.     DirectoryListBox1: TDirectoryListBox;
  22.     DriveComboBox1: TDriveComboBox;
  23.     FilterComboBox1: TFilterComboBox;
  24.     Panel1: TPanel;
  25.     pbPrevious: TButton;
  26.     pbNext: TButton;
  27.     pbConvert: TButton;
  28.     pbExit: TButton;
  29.     RadioGroup1: TRadioGroup;
  30.     Step3lLabel: TLabel;
  31.     Label3: TLabel;
  32.     Label4: TLabel;
  33.     Label5: TLabel;
  34.     Label1: TLabel;
  35.     Label2: TLabel;
  36.     Label6: TLabel;
  37.     Label7: TLabel;
  38.     pbAboutMe: TButton;
  39.     procedure pbConvertClick(Sender: TObject);
  40.     procedure pbExitClick(Sender: TObject);
  41.     procedure RadioGroup2Click(Sender: TObject);
  42.     procedure FormCreate(Sender: TObject);
  43.     Function ConvertFormOrText(FileToConvertFrom : string) : boolean ;
  44.     procedure ProcessAllFiles(const Extension : string) ;
  45.     procedure pbNextClick(Sender: TObject);
  46.     procedure pbPreviousClick(Sender: TObject);
  47.     procedure pbAboutMeClick(Sender: TObject);
  48.  
  49.   private
  50.  
  51.     { This holds the current type of conversion we are performing }
  52.  
  53.     ConversionType : TDFMOrTXT ;
  54.   public
  55.   end;
  56.  
  57. var
  58.   Form1: TForm1;
  59.  
  60. implementation
  61.  
  62. {$R *.DFM}
  63.  
  64. Function TForm1.ConvertFormOrText(FileToConvertFrom : string) : boolean ;
  65. Var
  66.   InputStream, OutputStream : TFileStream;
  67.   FileToConvertTo : string ;
  68. Begin
  69.  
  70.   { Given a file name this routine will convert the file from either
  71.     1. A text file to a DFM file or
  72.     2. A DFM file to a text file
  73.     The output file name is built from the input file name }
  74.  
  75.  
  76.   Result := True ;
  77.   FileToConvertTo := FileToConvertFrom ;
  78.  
  79.   { change file extensions as appropriate }
  80.  
  81.   case ConversionType of
  82.     ConvertToForm :
  83.     Begin
  84.       FileToConvertFrom := ChangeFileext(FileToConvertFrom, '.TXT') ;
  85.       FileToConvertTo := ChangeFileext(FileToConvertFrom, '.DFM') ;
  86.     End ;
  87.     ConvertToText :
  88.     Begin
  89.       FileToConvertFrom := ChangeFileext(FileToConvertFrom, '.DFM') ;
  90.       FileToConvertTo := ChangeFileext(FileToConvertFrom, '.TXT') ;
  91.     End ;
  92.   End ;
  93.  
  94.  
  95.   try
  96.  
  97.     try
  98.  
  99.       { Create a file stream for the specified file }
  100.  
  101.       InputStream  := TFileStream.Create(FileToConvertFrom, fmOpenRead);
  102.       OutputStream := TFileStream.Create(FileToConvertTo, fmCreate);
  103.  
  104.       { Now perform the selected conversion }
  105.  
  106.       case ConversionType of
  107.         ConvertToForm : ObjectTextToResource(InputStream, OutputStream) ;
  108.         ConvertToText : ObjectResourceToText(InputStream, OutputStream);
  109.       end ;
  110.  
  111.     Except
  112.       On EStreamError do Result := False ;
  113.       On EGPFault do result := False ;
  114.     End ;
  115.   Finally
  116.  
  117.     InputStream.Free;
  118.     OutputStream.Free;
  119.   End ;
  120. End;
  121.  
  122. procedure TForm1.ProcessAllFiles(const Extension : string) ;
  123. var
  124.   SourceFile : string ;
  125.   d : string ;
  126.   SearchRec: TSearchRec;
  127.   RetCode : integer ;
  128.   FilesConverted : Integer ;
  129. Begin
  130.  
  131.   { Routine to process all files of a specified type }
  132.  
  133.   FilesConverted := 0 ;
  134.  
  135.   d := DirectoryListBox1.directory+'\' ;
  136.  
  137.   RetCode := FindFirst(d+Extension, 0, SearchRec);
  138.   while RetCode = 0 do
  139.   Begin
  140.     SourceFile := d+SearchRec.Name ;
  141.     if ConvertFormOrText(SourceFile) then
  142.       Inc(FilesConverted) ;
  143.  
  144.     RetCode := FindNext(SearchRec) ;
  145.   end ;
  146.  
  147.   ResultLabel.Caption := inttostr(FilesConverted)+' file(s) converted correctly' ;
  148.  
  149. End ;
  150.  
  151. procedure TForm1.pbConvertClick(Sender: TObject);
  152. var
  153.   SourceFile : string ;
  154. begin
  155.  
  156.   { Choose whether to convert the selected file or
  157.     all the files }
  158.  
  159.   case RadioGroup1.ItemIndex of
  160.     0 :
  161.     Begin
  162.       SourceFile := FileListBox1.FileName ;
  163.       if fileexists(SourceFile) then
  164.       Begin
  165.         if ConvertFormOrText(SourceFile) then
  166.           ResultLabel.Caption := 'Done'
  167.         else
  168.           ResultLabel.caption := 'Failed to convert - may be invalid format'
  169.       End
  170.       else
  171.         ResultLabel.Caption := 'File Not Found' ;
  172.     end ;
  173.     1 :
  174.     Begin
  175.  
  176.       { Process all the files in the selected directory }
  177.  
  178.       case ConversionType of
  179.         ConvertToText : ProcessAllFiles('*.DFM') ;
  180.         ConvertToForm : ProcessAllFiles('*.TXT') ;
  181.       End ;
  182.     End ;
  183.   end ;
  184. end;
  185.  
  186. procedure TForm1.pbExitClick(Sender: TObject);
  187. begin
  188.   close ;
  189. end;
  190.  
  191. procedure TForm1.RadioGroup2Click(Sender: TObject);
  192. begin
  193.  
  194.   { Swap TXT to DFM or DFM to TXT }
  195.  
  196.   case RadioGroup2.itemIndex of
  197.    0 :
  198.     Begin
  199.       pbConvert.caption := 'DFM -> TXT' ;
  200.       FileListBox1.Mask := '*.DFM' ;
  201.       FilterComboBox1.Filter := 'Form Files|*.DFM' ;
  202.       ConversionType := ConvertToText ;
  203.     End ;
  204.     1 :
  205.     Begin
  206.       pbConvert.caption := 'TXT -> DFM' ;
  207.       FileListBox1.Mask := '*.TXT' ;
  208.       FilterComboBox1.Filter := 'Text Files|*.TXT' ;
  209.       ConversionType := ConvertToForm ;
  210.     End ;
  211.   end ;
  212. end;
  213.  
  214. procedure TForm1.FormCreate(Sender: TObject);
  215. begin
  216.   ConversionType := ConvertToText ;
  217.   pbConvert.caption := 'DFM -> TXT' ;
  218.   FileListBox1.Mask := '*.DFM' ;
  219. end;
  220.  
  221. procedure TForm1.pbNextClick(Sender: TObject);
  222. begin
  223.  
  224.   { Next screen button }
  225.  
  226.   pbConvert.Enabled := false ;
  227.   pbNext.Enabled := True ;
  228.   pbPrevious.Enabled := True ;
  229.   case Wizard.PageIndex of
  230.     0 :
  231.     Begin
  232.       Wizard.PageIndex := Wizard.PageIndex + 1 ;
  233.     End ;
  234.     1 :
  235.     Begin
  236.       Wizard.PageIndex := Wizard.PageIndex + 1 ;
  237.       pbConvert.Enabled := True ;
  238.       pbNext.enabled := false ;
  239.  
  240.     End ;
  241.   end ;
  242. end;
  243.  
  244. procedure TForm1.pbPreviousClick(Sender: TObject);
  245. begin
  246.  
  247.   { Previous Screen button }
  248.  
  249.   pbConvert.Enabled := false ;
  250.   pbNext.Enabled := True ;
  251.   pbPrevious.Enabled := True ;
  252.   case Wizard.PageIndex of
  253.     1 :
  254.     Begin
  255.       Wizard.PageIndex := Wizard.PageIndex - 1 ;
  256.       pbPrevious.enabled := false ;
  257.     End ;
  258.     2 :
  259.     Begin
  260.       Wizard.PageIndex := Wizard.PageIndex - 1 ;
  261.     End ;
  262.   end ;
  263.  
  264. end;
  265.  
  266. procedure TForm1.pbAboutMeClick(Sender: TObject);
  267. begin
  268.   MessageDlg('Written by John Wright (CIS 100335,322) (c) 1995.'+#13+'Use at your own risk.', mtInformation, [mbOK], 0) ;
  269. end;
  270.  
  271. end.
  272.